// 模块加载器 - 统一管理JS模块加载
class ModuleLoader {
    constructor() {
        this.loadedModules = new Set();
        this.moduleCallbacks = new Map();
    }

    // 加载模块
    async loadModule(moduleName, modulePath) {
        if (this.loadedModules.has(moduleName)) {
            return Promise.resolve();
        }

        return new Promise((resolve, reject) => {
            const script = document.createElement('script');
            script.src = modulePath;
            script.onload = () => {
                this.loadedModules.add(moduleName);
                resolve();
            };
            script.onerror = reject;
            document.head.appendChild(script);
        });
    }

    // 批量加载模块
    async loadModules(modules) {
        const promises = modules.map(([name, path]) => 
            this.loadModule(name, path)
        );
        return Promise.all(promises);
    }

    // 注册模块回调
    registerCallback(moduleName, callback) {
        if (!this.moduleCallbacks.has(moduleName)) {
            this.moduleCallbacks.set(moduleName, []);
        }
        this.moduleCallbacks.get(moduleName).push(callback);
    }

    // 执行模块回调
    executeCallbacks(moduleName) {
        const callbacks = this.moduleCallbacks.get(moduleName) || [];
        callbacks.forEach(callback => callback());
    }
}

// 全局模块加载器实例
window.moduleLoader = new ModuleLoader();